package StudyCode.summary.myfile;

import java.nio.file.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class WatchModifyFileOrDirectory {
	public static void main(String[] args) throws Exception {
		Path path = Paths.get("E:/Copy");
		try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
			path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
			while (true) {
				WatchKey key = watchService.take();
				for (WatchEvent<?> watchEvent : key.pollEvents()) {
					WatchEvent.Kind<?> kind = watchEvent.kind();
					if (kind == StandardWatchEventKinds.OVERFLOW) {
						continue;
					}
					WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
					Path filename = watchEventPath.context();
					System.out.println(kind + " -> " + filename);
				}
				boolean valid = key.reset();
				if (!valid) {
					break;
				}
			}
		}
	}
}
